﻿using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.DebugViews;

namespace UtilZ.Dotnet.Ex.Model
{
#pragma warning disable 8500 // address / sizeof of managed types

    /// <summary>
    /// 非托管内存指针
    /// </summary>
    [DebuggerTypeProxy(typeof(StructSequenceDebugView<>))]
    [DebuggerDisplay("Length = {Length}")]
    //[DebuggerDisplay("{ToString(),raw}")]
    unsafe public sealed class UnmanagedMemoryPointer<T> : UnmanagedMemoryPointerAbs
        where T : struct
    {
        /// <summary>
        /// 指针
        /// </summary>
        private T* _dataPtr = null;
        /// <summary>
        /// 指针
        /// </summary>
        public T* DataPtr
        {
#if !NET4_0
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
            get { return this._dataPtr; }
        }






        /// <summary>
        /// 构造函数
        /// </summary>
        public UnmanagedMemoryPointer()
            : base(sizeof(T), 0L, UnmanagedMemoryType.Revisable)
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="elementCount">初始化元素个数</param>
        public UnmanagedMemoryPointer(long elementCount)
            : base(sizeof(T), elementCount, UnmanagedMemoryType.Revisable)
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dataPtr">指针</param>
        /// <param name="elementCount">元素个数</param>
        public UnmanagedMemoryPointer(T* dataPtr, long elementCount)
            : base(sizeof(T), elementCount, UnmanagedMemoryType.Fix)
        {
            this._dataPtr = dataPtr;
        }




        /// <summary>
        /// 获取或设置指定索引处的值
        /// </summary>
        /// <param name="index">索引</param>
        /// <returns></returns>
        public ref T this[long index]
        {
#if !NET4_0
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
            get
            {
                if (index < 0 || index >= base._longCapacity)
                {
                    throw new IndexOutOfRangeException($"索引越界,index:{index},当前数据长度:{this.LongLength}");
                }

                return ref *(this._dataPtr + index);
            }
            //[MethodImpl(MethodImplOptions.AggressiveInlining)]
            //set
            //{
            //    if (index < 0 || index >= base._longCapacity)
            //    {
            //        throw new IndexOutOfRangeException();
            //    }

            //    *(this._dataPtr + index) = value;
            //}
        }

        /// <summary>
        /// 获取或设置指定索引处的值
        /// </summary>
        /// <param name="index">索引</param>
        /// <returns></returns>
        public ref T this[int index]
        {
#if !NET4_0
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
            get
            {
                if (index < 0 || index >= base._longCapacity)
                {
                    throw new IndexOutOfRangeException($"索引越界,index:{index},当前数据长度:{this.LongLength}");
                }

                return ref *(this._dataPtr + index);
            }
            //[MethodImpl(MethodImplOptions.AggressiveInlining)]
            //set
            //{
            //    if (index < 0 || index >= base._longCapacity)
            //    {
            //        throw new IndexOutOfRangeException();
            //    }

            //    *(this._dataPtr + index) = value;
            //}
        }


        /// <summary>
        /// 获取Span泛型实例
        /// </summary>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public Span<T> GetSpan()
        {
            return new Span<T>(this._dataPtr, base._length);
        }

        /// <summary>
        /// 获取Span泛型实例
        /// </summary>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public Span<T> GetSpan(int length)
        {
            if (base._capacity < length)
            {
                base.CheckSetLength(length);
            }
            return new Span<T>(this._dataPtr, length);
        }

        /// <summary>
        /// 获取Span泛型实例
        /// </summary>
        /// <param name="startIndex">指针数据起始位置</param>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public Span<T> GetSpan(int startIndex, int length)
        {
            if (base._capacity - startIndex < length)
            {
                base.CheckSetLength(startIndex + length);
            }

            return new Span<T>(this._dataPtr + startIndex, length);
        }

        /// <summary>
        /// 获取Span泛型实例
        /// </summary>
        /// <param name="startIndex">指针数据起始位置</param>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public Span<T> GetSpan(long startIndex, int length)
        {
            if (base._longCapacity - startIndex < length)
            {
                base.CheckSetLength(startIndex + length);
            }

            return new Span<T>(this._dataPtr + startIndex, length);
        }




        /// <summary>
        /// 获取SpanZ泛型实例
        /// </summary>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public SpanZ<T> GetSpanZ()
        {
            return new SpanZ<T>(this._dataPtr, base._length);
        }

        /// <summary>
        /// 获取SpanZ泛型实例
        /// </summary>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public SpanZ<T> GetSpanZ(int length)
        {
            if (base._capacity < length)
            {
                base.CheckSetLength(length);
            }
            return new SpanZ<T>(this._dataPtr, length);
        }

        /// <summary>
        /// 获取Span泛型实例
        /// </summary>
        /// <param name="startIndex">指针数据起始位置</param>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public SpanZ<T> GetSpanZ(int startIndex, int length)
        {
            if (base._capacity - startIndex < length)
            {
                base.CheckSetLength(startIndex + length);
            }

            return new SpanZ<T>(this._dataPtr + startIndex, length);
        }

        /// <summary>
        /// 获取Span泛型实例
        /// </summary>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public SpanZ<T> GetSpanZ(long length)
        {
            if (base._longCapacity < length)
            {
                base.CheckSetLength(length);
            }
            return new SpanZ<T>(this._dataPtr, length);
        }

        /// <summary>
        /// 获取Span泛型实例
        /// </summary>
        /// <param name="startIndex">指针数据起始位置</param>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public SpanZ<T> GetSpanZ(long startIndex, long length)
        {
            if (base._longCapacity - startIndex < length)
            {
                base.CheckSetLength(startIndex + length);
            }

            return new SpanZ<T>(this._dataPtr + startIndex, length);
        }

        /// <summary>
        /// 将当前类型强制转换为目标的类型
        /// </summary>
        /// <typeparam name="TResult">目标类型</typeparam>
        /// <returns>目标类型SpanZ</returns>
        public SpanZ<TResult> Cast<TResult>() where TResult : struct
        {
            return new SpanZ<TResult>(this.DataPtr, this.LongLength * sizeof(T) / sizeof(TResult));
        }




        /// <summary>
        /// 转换为LocalSpanZ表示的当前内存引用
        /// </summary>
        /// <returns>LocalSpanZ表示的当前内存引用</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public LocalSpanZ<T> GetLocalSpanZ()
        {
            return new LocalSpanZ<T>(this._dataPtr, base._longLength);
        }

        /// <summary>
        /// 获取SpanZ泛型实例
        /// </summary>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public LocalSpanZ<T> GetLocalSpanZ(int length)
        {
            if (base._capacity < length)
            {
                base.CheckSetLength(length);
            }
            return new LocalSpanZ<T>(this._dataPtr, length);
        }

        /// <summary>
        /// 获取Span泛型实例
        /// </summary>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public LocalSpanZ<T> GetLocalSpanZ(long length)
        {
            if (base._longCapacity < length)
            {
                base.CheckSetLength(length);
            }
            return new LocalSpanZ<T>(this._dataPtr, length);
        }

        /// <summary>
        /// 获取Span泛型实例
        /// </summary>
        /// <param name="startIndex">指针数据起始位置</param>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public LocalSpanZ<T> GetLocalSpanZ(int startIndex, int length)
        {
            if (base._capacity - startIndex < length)
            {
                base.CheckSetLength(startIndex + length);
            }

            return new LocalSpanZ<T>(this._dataPtr + startIndex, length);
        }

        /// <summary>
        /// 获取Span泛型实例
        /// </summary>
        /// <param name="startIndex">指针数据起始位置</param>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public LocalSpanZ<T> GetLocalSpanZ(long startIndex, long length)
        {
            if (base._longCapacity - startIndex < length)
            {
                base.CheckSetLength(startIndex + length);
            }

            return new LocalSpanZ<T>(this._dataPtr + startIndex, length);
        }





        /// <summary>
        /// 创建非托管数据流UnmanagedMemoryStream,需要外部释放流资源,失败将会抛出异常.
        /// </summary>
        /// <param name="access">流访问类型,只读或是只写还是即可读也可写</param>
        /// <returns>创建成功的流</returns>
        public Stream CreateStream(FileAccess access = FileAccess.ReadWrite)
        {
            SpanZ<byte> buffer = this.Cast<byte>();
            return new UnmanagedMemoryStream(buffer.DataPtr, buffer.Length, buffer.Length, access);
        }

        /// <summary>
        /// 创建BinaryReader,需要外部释放流资源,失败将会抛出异常.
        /// </summary>
        /// <returns>创建成功的BinaryReader</returns>
        public BinaryReader CreateReader()
        {
            SpanZ<byte> buffer = this.Cast<byte>();
            return new BinaryReader(new UnmanagedMemoryStream(buffer.DataPtr, buffer.Length, buffer.Length, FileAccess.Read));
        }

        /// <summary>
        /// 创建BinaryWriter,需要外部释放流资源,失败将会抛出异常.
        /// </summary>
        /// <returns>创建成功的BinaryWriter</returns>
        public BinaryWriter CreateWriter()
        {
            SpanZ<byte> buffer = this.Cast<byte>();
            return new BinaryWriter(new UnmanagedMemoryStream(buffer.DataPtr, buffer.Length, buffer.Length, FileAccess.Write));
        }





        /// <summary>
        /// 获取柔性数组实例
        /// </summary>
        /// <param name="length">指定的目标数组长度</param>
        /// <returns>柔性数组实例</returns>
        public ValueTypeFlexibleArray<T> GetValueTypeFlexibleArray(int length)
        {
            if (base._capacity < length)
            {
                base.CheckSetLength(length);
            }
            return new ValueTypeFlexibleArray<T>(this._dataPtr, length);
        }

        /// <summary>
        /// 获取柔性数组实例
        /// </summary>
        /// <param name="startIndex">指定的目标数组起始位置</param>
        /// <param name="length">指定的目标数组长度</param>
        /// <returns></returns>
        public ValueTypeFlexibleArray<T> GetValueTypeFlexibleArray(int startIndex, int length)
        {
            if (base._capacity - startIndex < length)
            {
                base.CheckSetLength(startIndex + length);
            }

            return new ValueTypeFlexibleArray<T>(this._dataPtr + startIndex, length);
        }




        /// <summary>
        /// 将源数据复制到目标数据中,不作参数检查
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="destination">目标数据</param>
        public static void Copy<ElelmentType>(UnmanagedMemoryPointer<ElelmentType> source, UnmanagedMemoryPointer<ElelmentType> destination)
            where ElelmentType : struct
        {
            NativeMemory.Copy(source._dataPtr, destination._dataPtr, (nuint)(source._longLength * sizeof(ElelmentType)));
        }

        /// <summary>
        /// 将源数据复制到目标数据中,不作参数检查
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="soucrceStartIndex">当前数据起始位置</param>
        /// <param name="destination">目标指针</param>
        /// <param name="destinationStartIndex">目标数据起始位置</param>
        /// <param name="count">复制元素个数</param>
        public static void Copy<ElelmentType>(UnmanagedMemoryPointer<ElelmentType> source, int soucrceStartIndex, UnmanagedMemoryPointer<ElelmentType> destination, int destinationStartIndex, int count)
            where ElelmentType : struct
        {
            NativeMemory.Copy(source._dataPtr + soucrceStartIndex, destination._dataPtr + destinationStartIndex, (nuint)((long)count * sizeof(ElelmentType)));
        }

        /// <summary>
        /// 将源数据复制到目标数据中,不作参数检查
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="soucrceStartIndex">当前数据起始位置</param>
        /// <param name="destination">目标数据</param>
        /// <param name="destinationStartIndex">目标数据起始位置</param>
        /// <param name="count">复制元素个数</param>
        public static void Copy<ElelmentType>(UnmanagedMemoryPointer<ElelmentType> source, long soucrceStartIndex, UnmanagedMemoryPointer<ElelmentType> destination, long destinationStartIndex, long count)
            where ElelmentType : struct
        {
            NativeMemory.Copy(source._dataPtr + soucrceStartIndex, destination._dataPtr + destinationStartIndex, (nuint)(count * sizeof(ElelmentType)));
        }

        /// <summary>
        /// 将源数据复制到目标数据中,不作参数检查
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="soucrceStartIndex">当前数据起始位置</param>
        /// <param name="destination">目标数据</param>
        /// <param name="destinationStartIndex">目标数据起始位置</param>
        /// <param name="count">复制元素个数</param>
        public static void Copy<ElelmentType>(ElelmentType[] source, int soucrceStartIndex, UnmanagedMemoryPointer<ElelmentType> destination, long destinationStartIndex, long count)
            where ElelmentType : struct
        {
            fixed (ElelmentType* ptr = source)
            {
                NativeMemory.Copy(ptr + soucrceStartIndex, destination._dataPtr + destinationStartIndex, (nuint)(count * sizeof(ElelmentType)));
            }

        }

        /// <summary>
        /// 将源数据复制到目标数据中,不作参数检查
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="soucrceStartIndex">当前数据起始位置</param>
        /// <param name="destination">目标数据</param>
        /// <param name="destinationStartIndex">目标数据起始位置</param>
        /// <param name="count">复制元素个数</param>
        public static void Copy<ElelmentType>(ElelmentType[] source, long soucrceStartIndex, UnmanagedMemoryPointer<ElelmentType> destination, long destinationStartIndex, long count)
            where ElelmentType : struct
        {
            fixed (ElelmentType* ptr = source)
            {
                NativeMemory.Copy(ptr + soucrceStartIndex, destination._dataPtr + destinationStartIndex, (nuint)(count * sizeof(ElelmentType)));
            }
        }

        /// <summary>
        /// 将当前数据复制到目标数据中,作参数检查
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="soucrceStartIndex">当前数据起始位置</param>
        /// <param name="destination">目标数据</param>
        public static void Copy<ElelmentType>(UnmanagedMemoryPointer<ElelmentType> source, int soucrceStartIndex, Span<ElelmentType> destination)
              where ElelmentType : struct
        {
#if NET4_0
            void* destPtr = destination.DataPtr;
#else
            void* destPtr = Unsafe.AsPointer(ref destination[0]);
#endif
            NativeMemory.Copy(source._dataPtr + soucrceStartIndex, destPtr, new nuint((ulong)(destination.Length * sizeof(ElelmentType))));
        }

        /// <summary>
        /// 将当前数据复制到目标数据中,作参数检查
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="soucrceStartIndex">当前数据起始位置</param>
        /// <param name="destination">目标数据</param>
        public static void Copy<ElelmentType>(UnmanagedMemoryPointer<ElelmentType> source, int soucrceStartIndex, SpanZ<ElelmentType> destination)
              where ElelmentType : struct
        {
            NativeMemory.Copy(source._dataPtr + soucrceStartIndex, destination.DataPtr, new nuint((ulong)(destination.Length * sizeof(ElelmentType))));
        }

        /// <summary>
        /// 将当前数据复制到目标数据中,作参数检查
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="soucrceStartIndex">当前数据起始位置</param>
        /// <param name="destination">目标数据</param>
        public static void Copy<ElelmentType>(UnmanagedMemoryPointer<ElelmentType> source, long soucrceStartIndex, SpanZ<ElelmentType> destination)
              where ElelmentType : struct
        {
            NativeMemory.Copy(source._dataPtr + soucrceStartIndex, destination.DataPtr, new nuint((ulong)(destination.LongLength * sizeof(ElelmentType))));
        }





        /// <summary>
        /// 将当前数据复制到目标数据中,作参数检查
        /// </summary>
        /// <param name="destination">目标数据</param>
        public void CopyTo(T[] destination)
        {
            if (destination == null)
            {
                throw new ArgumentOutOfRangeException(nameof(destination));
            }

            if (destination.LongLength < base._longLength)
            {
                throw new ArgumentOutOfRangeException(nameof(destination), "目标容量不足");
            }

            fixed (T* destPtr = destination)
            {
                NativeMemory.Copy(this._dataPtr, destPtr, (nuint)(base._longLength * sizeof(T)));
            }
        }

        /// <summary>
        /// 将当前数据复制到目标数据中,作参数检查
        /// </summary>
        /// <param name="soucrceStartIndex">当前数据起始位置</param>
        /// <param name="destination">目标数据</param>
        /// <param name="destinationStartIndex">目标数据起始位置</param>
        /// <param name="destinationCount">拷贝元素个数</param>
        public void CopyTo(int soucrceStartIndex, T[] destination, int destinationStartIndex, int destinationCount)
        {
            if (destination == null)
            {
                throw new ArgumentOutOfRangeException(nameof(destination));
            }

            if (this._longCapacity < destinationCount)
            {
                throw new ArgumentOutOfRangeException(nameof(destinationCount), "数据源长度不足");
            }

            fixed (T* destPtr = destination)
            {
                NativeMemory.Copy(this._dataPtr + soucrceStartIndex, destPtr + destinationStartIndex, (nuint)(destinationCount * sizeof(T)));
            }
        }

        /// <summary>
        /// 将当前数据复制到目标数据中,作参数检查
        /// </summary>
        /// <param name="destination">目标数据</param>
        public void CopyTo(UnmanagedMemoryPointer<T> destination)
        {
            if (destination == null)
            {
                throw new ArgumentOutOfRangeException(nameof(destination));
            }

            if (destination._longCapacity < base._longLength)
            {
                throw new ArgumentOutOfRangeException(nameof(destination), "目标容量不足");
            }

            NativeMemory.Copy(this._dataPtr, destination._dataPtr, (nuint)(base._longLength * sizeof(T)));
        }

        /// <summary>
        /// 将当前数据复制到目标数据中,作参数检查
        /// </summary>
        /// <param name="soucrceStartIndex">当前数据起始位置</param>
        /// <param name="destination">目标数据</param>
        /// <param name="destinationStartIndex">目标数据起始位置</param>
        /// <param name="count">复制元素个数</param>
        public void CopyTo(int soucrceStartIndex, UnmanagedMemoryPointer<T> destination, int destinationStartIndex, int count)
        {
            if (soucrceStartIndex < 0 || soucrceStartIndex >= base._length)
            {
                throw new ArgumentOutOfRangeException(nameof(soucrceStartIndex), "复制的源数据起始位置超出了当前数据容量范围");
            }

            long soucrceStartIndexL = soucrceStartIndex;
            long countL = count;
            if (soucrceStartIndexL + countL > base._longLength)
            {
                throw new ArgumentOutOfRangeException(nameof(soucrceStartIndex), "复制的数据长度超出了当前数据容量");
            }

            if (destination == null)
            {
                throw new ArgumentOutOfRangeException(nameof(destination));
            }

            long destinationStartIndexL = destinationStartIndex;
            if (destinationStartIndexL < 0 || destinationStartIndexL >= destination._longCapacity)
            {
                throw new ArgumentOutOfRangeException(nameof(destinationStartIndex), "复制的目标数据起始位置超出了当前数据容量范围");
            }

            if (destinationStartIndex + countL > destination._longCapacity)
            {
                throw new ArgumentOutOfRangeException(nameof(count), "复制的数据长度超出了目标数据容量");
            }

            NativeMemory.Copy(this._dataPtr + soucrceStartIndexL, destination._dataPtr + destinationStartIndexL, new nuint((ulong)(countL * sizeof(T))));
        }

        /// <summary>
        /// 将当前数据复制到目标数据中,作参数检查
        /// </summary>
        /// <param name="soucrceStartIndex">当前数据起始位置</param>
        /// <param name="destination">目标数据</param>
        /// <param name="destinationStartIndex">目标数据起始位置</param>
        /// <param name="count">复制元素个数</param>
        public void CopyTo(long soucrceStartIndex, UnmanagedMemoryPointer<T> destination, long destinationStartIndex, long count)
        {
            if (soucrceStartIndex < 0 || soucrceStartIndex >= base._longLength)
            {
                throw new ArgumentOutOfRangeException(nameof(soucrceStartIndex), "复制的源数据起始位置超出了当前数据容量范围");
            }

            if (soucrceStartIndex + count > base._longLength)
            {
                throw new ArgumentOutOfRangeException(nameof(soucrceStartIndex), "复制的数据长度超出了当前数据容量");
            }

            if (destination == null)
            {
                throw new ArgumentOutOfRangeException(nameof(destination));
            }

            if (destinationStartIndex < 0 || destinationStartIndex >= destination._longCapacity)
            {
                throw new ArgumentOutOfRangeException(nameof(destinationStartIndex), "复制的目标数据起始位置超出了当前数据容量范围");
            }

            if (destinationStartIndex + count > destination._longCapacity)
            {
                throw new ArgumentOutOfRangeException(nameof(count), "复制的数据长度超出了目标数据容量");
            }

            NativeMemory.Copy(this._dataPtr + soucrceStartIndex, destination._dataPtr + destinationStartIndex, new nuint((ulong)(count * sizeof(T))));
        }

        /// <summary>
        /// 将当前数据复制到目标数据中,作参数检查
        /// </summary>
        /// <param name="soucrceStartIndex">当前数据起始位置</param>
        /// <param name="destination">目标数据</param>
        public void CopyTo(int soucrceStartIndex, Span<T> destination)
        {
            if (soucrceStartIndex < 0 || soucrceStartIndex >= base._longLength)
            {
                throw new ArgumentOutOfRangeException(nameof(soucrceStartIndex), "复制的源数据起始位置超出了当前数据容量范围");
            }

            if (base._longLength - soucrceStartIndex < destination.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(destination), "复制的数据长度超出了目标数据容量");
            }

#if NET4_0
            void* destPtr = destination.DataPtr;
#else
            void* destPtr = Unsafe.AsPointer(ref destination[0]);
#endif
            NativeMemory.Copy(this._dataPtr + soucrceStartIndex, destPtr, new nuint((ulong)(destination.Length * sizeof(T))));
        }

        /// <summary>
        /// 将当前数据复制到目标数据中,作参数检查
        /// </summary>
        /// <param name="soucrceStartIndex">当前数据起始位置</param>
        /// <param name="destination">目标数据</param>
        public void CopyTo(int soucrceStartIndex, SpanZ<T> destination)
        {
            if (soucrceStartIndex < 0 || soucrceStartIndex >= base._longLength)
            {
                throw new ArgumentOutOfRangeException(nameof(soucrceStartIndex), "复制的源数据起始位置超出了当前数据容量范围");
            }

            if (base._longLength - soucrceStartIndex < destination.LongLength)
            {
                throw new ArgumentOutOfRangeException(nameof(destination), "复制的数据长度超出了目标数据容量");
            }

            NativeMemory.Copy(this._dataPtr + soucrceStartIndex, destination.DataPtr, new nuint((ulong)(destination.Length * sizeof(T))));
        }

        /// <summary>
        /// 将当前数据复制到目标数据中,作参数检查
        /// </summary>
        /// <param name="soucrceStartIndex">当前数据起始位置</param>
        /// <param name="destination">目标数据</param>
        public void CopyTo(long soucrceStartIndex, SpanZ<T> destination)
        {
            if (soucrceStartIndex < 0 || soucrceStartIndex >= base._longLength)
            {
                throw new ArgumentOutOfRangeException(nameof(soucrceStartIndex), "复制的源数据起始位置超出了当前数据容量范围");
            }

            if (base._longLength - soucrceStartIndex < destination.LongLength)
            {
                throw new ArgumentOutOfRangeException(nameof(destination), "复制的数据长度超出了目标数据容量");
            }

            NativeMemory.Copy(this._dataPtr + soucrceStartIndex, destination.DataPtr, new nuint((ulong)(destination.LongLength * sizeof(T))));
        }




        /// <summary>
        /// 调整指针指向的内存大小,调整成功返回true,调整失败返回false
        /// </summary>
        /// <param name="byteLength">要申请的内存大小字节数</param>
        /// <param name="error">调整失败时的错误信息</param>
        /// <returns>调整成功返回true,调整失败返回false</returns>
        protected override bool PrimitiveCheck(long byteLength, out string error)
        {
            void* newPtr;
            var byteCount = new nuint((ulong)byteLength);

            if (this._dataPtr == null)
            {
                newPtr = NativeMemory.Alloc(byteCount);
            }
            else
            {
                newPtr = NativeMemory.Realloc(this._dataPtr, byteCount);
            }

            if (newPtr == null)
            {
                error = $"申请大小为{byteLength}Byte的内存失败.";
                return false;
            }
            else
            {
                this._dataPtr = (T*)newPtr;
                error = null;
                return true;
            }
        }

        /// <summary>
        /// 释放指针
        /// </summary>
        protected override void PrimitiveFree()
        {
            if (this._dataPtr != null)
            {
                NativeMemory.Free(this._dataPtr);
                this._dataPtr = null;
            }
        }

        /// <summary>
        /// 清除当前对象数据指针指向的内容。
        /// </summary>
        protected override void PrimitiveClear()
        {
            NativeMemory.Clear(this._dataPtr, (nuint)(base._elementSize * base._longCapacity));
        }

        /// <summary>
        /// 用指定的值填充到当前对象数据指针指向的内容
        /// </summary>
        /// <typeparam name="TValue">值类型</typeparam>
        /// <param name="value"></param>
#if CORE
        protected override void PrimitiveFill<TValue>(TValue value) where TValue : struct
#else
        protected override void PrimitiveFill<TValue>(TValue value)
#endif
        {
            UnmanagedMemoryHelper.Fill(this._dataPtr, base._longCapacity, value);
        }

        /// <summary>
        /// 重写ToString
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.GetSpanZ().ToString();
        }
    }

    /// <summary>
    /// IQ数据非托管内存指针
    /// </summary>
    [DebuggerTypeProxy(typeof(IQSequenceDebugView<>))]
    [DebuggerDisplay("Length = {Length}")]
    unsafe public sealed class IQUnmanagedMemoryPointer<T> : UnmanagedMemoryPointerAbs
        where T : struct
    {
        private T* _iDataPtr = null;
        /// <summary>
        /// I路数据指针
        /// </summary>
        public T* IDataPtr
        {
#if !NET4_0
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
            get { return this._iDataPtr; }
        }


        private T* _qDataPtr = null;
        /// <summary>
        /// Q路数据指针
        /// </summary>
        public T* QDataPtr
        {
#if !NET4_0
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
            get { return this._qDataPtr; }
        }


        /// <summary>
        /// 获取I路数据Span
        /// </summary>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public Span<T> GetISpan()
        {
            return new Span<T>(this._iDataPtr, base._length);
        }

        /// <summary>
        /// 获取Q路数据Span
        /// </summary>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public Span<T> GetQSpan()
        {
            return new Span<T>(this._qDataPtr, base._length);
        }

        /// <summary>
        /// 获取I路数据Span
        /// </summary>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public Span<T> GetISpan(int length)
        {
            if (base._capacity < length)
            {
                base.CheckSetLength(length);
            }

            return new Span<T>(this._iDataPtr, length);
        }

        /// <summary>
        /// 获取Q路数据Span
        /// </summary>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public Span<T> GetQSpan(int length)
        {
            if (base._capacity < length)
            {
                base.CheckSetLength(length);
            }

            return new Span<T>(this._qDataPtr, length);
        }

        /// <summary>
        /// 获取I路数据Span
        /// </summary>
        /// <param name="startIndex">指针数据起始位置</param>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public Span<T> GetISpan(int startIndex, int length)
        {
            if (base._longCapacity - startIndex < length)
            {
                base.CheckSetLength((long)startIndex + (long)length);
            }

            return new Span<T>(this._iDataPtr + startIndex, length);
        }

        /// <summary>
        /// 获取Q路数据Span
        /// </summary>
        /// <param name="startIndex">指针数据起始位置</param>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public Span<T> GetQSpan(int startIndex, int length)
        {
            if (base._longCapacity - startIndex < length)
            {
                base.CheckSetLength((long)startIndex + (long)length);
            }

            return new Span<T>(this._qDataPtr + startIndex, length);
        }

        /// <summary>
        /// 获取I路数据Span
        /// </summary>
        /// <param name="startIndex">指针数据起始位置</param>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public Span<T> GetISpan(long startIndex, int length)
        {
            if (base._longCapacity - startIndex < length)
            {
                base.CheckSetLength(startIndex + (long)length);
            }

            return new Span<T>(this._iDataPtr + startIndex, length);
        }

        /// <summary>
        /// 获取Q路数据Span
        /// </summary>
        /// <param name="startIndex">指针数据起始位置</param>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public Span<T> GetQSpan(long startIndex, int length)
        {
            if (base._longCapacity - startIndex < length)
            {
                base.CheckSetLength(startIndex + (long)length);
            }

            return new Span<T>(this._qDataPtr + startIndex, length);
        }




        /// <summary>
        /// 获取I路数据SpanZ
        /// </summary>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public SpanZ<T> GetISpanZ()
        {
            return new SpanZ<T>(this._iDataPtr, base._length);
        }

        /// <summary>
        /// 获取Q路数据SpanZ
        /// </summary>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public SpanZ<T> GetQSpanZ()
        {
            return new SpanZ<T>(this._qDataPtr, base._length);
        }

        /// <summary>
        /// 获取I路数据SpanZ
        /// </summary>
        /// <param name="length">I路数据长度</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public SpanZ<T> GetISpanZ(int length)
        {
            if (base._capacity < length)
            {
                base.CheckSetLength(length);
            }

            return new SpanZ<T>(this._iDataPtr, length);
        }

        /// <summary>
        /// 获取Q路数据SpanZ
        /// </summary>
        /// <param name="length">Q路数据长度</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public SpanZ<T> GetQSpanZ(int length)
        {
            if (base._capacity < length)
            {
                base.CheckSetLength(length);
            }

            return new SpanZ<T>(this._qDataPtr, length);
        }

        /// <summary>
        /// 获取I路数据SpanZ
        /// </summary>
        /// <param name="length">I路数据长度</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public SpanZ<T> GetISpanZ(long length)
        {
            if (base._longCapacity < length)
            {
                base.CheckSetLength(length);
            }

            return new SpanZ<T>(this._iDataPtr, length);
        }

        /// <summary>
        /// 获取Q路数据SpanZ
        /// </summary>
        /// <param name="length">Q路数据长度</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public SpanZ<T> GetQSpanZ(long length)
        {
            if (base._longCapacity < length)
            {
                base.CheckSetLength(length);
            }

            return new SpanZ<T>(this._qDataPtr, length);
        }

        /// <summary>
        /// 获取I路数据SpanZ
        /// </summary>
        /// <param name="startIndex">指针数据起始位置</param>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public SpanZ<T> GetISpanZ(int startIndex, int length)
        {
            if (base._longCapacity - startIndex < length)
            {
                base.CheckSetLength((long)startIndex + (long)length);
            }

            return new SpanZ<T>(this._iDataPtr + startIndex, length);
        }

        /// <summary>
        /// 获取Q路数据SpanZ
        /// </summary>
        /// <param name="startIndex">指针数据起始位置</param>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public SpanZ<T> GetQSpanZ(int startIndex, int length)
        {
            if (base._longCapacity - startIndex < length)
            {
                base.CheckSetLength((long)startIndex + (long)length);
            }

            return new SpanZ<T>(this._qDataPtr + startIndex, length);
        }

        /// <summary>
        /// 获取I路数据SpanZ
        /// </summary>
        /// <param name="startIndex">指针数据起始位置</param>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public SpanZ<T> GetISpanZ(long startIndex, long length)
        {
            if (base._longCapacity - startIndex < length)
            {
                base.CheckSetLength(startIndex + length);
            }

            return new SpanZ<T>(this._iDataPtr + startIndex, length);
        }

        /// <summary>
        /// 获取Q路数据SpanZ
        /// </summary>
        /// <param name="startIndex">指针数据起始位置</param>
        /// <param name="length">获取指定长度的Span</param>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public SpanZ<T> GetQSpanZ(long startIndex, long length)
        {
            if (base._longCapacity - startIndex < length)
            {
                base.CheckSetLength(startIndex + length);
            }

            return new SpanZ<T>(this._qDataPtr + startIndex, length);
        }








        /// <summary>
        /// 获取IQSpan泛型实例
        /// </summary>
        /// <returns>IQSpan</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public IQSpanZ<T> GetIQSpanZ()
        {
            return new IQSpanZ<T>(this._iDataPtr, this._qDataPtr, base._length);
        }

        /// <summary>
        /// 获取IQSpan泛型实例
        /// </summary>
        /// <param name="length">IQSpan长度</param>
        /// <returns>IQSpan</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public IQSpanZ<T> GetIQSpanZ(int length)
        {
            if (base._capacity < length)
            {
                base.CheckSetLength(length);
            }

            return new IQSpanZ<T>(this._iDataPtr, this._qDataPtr, length);
        }

        /// <summary>
        /// 获取IQSpan泛型实例
        /// </summary>
        /// <param name="length">IQSpan长度</param>
        /// <returns>IQSpan</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public IQSpanZ<T> GetIQSpanZ(long length)
        {
            if (base._longCapacity < length)
            {
                base.CheckSetLength(length);
            }

            return new IQSpanZ<T>(this._iDataPtr, this._qDataPtr, length);
        }

        /// <summary>
        /// 获取IQSpan泛型实例
        /// </summary>
        /// <param name="startIndex">指针数据起始位置</param>
        /// <param name="length">IQSpan长度</param>
        /// <returns>IQSpan</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public IQSpanZ<T> GetIQSpanZ(int startIndex, int length)
        {
            if (base._capacity - startIndex < length)
            {
                base.CheckSetLength(startIndex + length);
            }

            return new IQSpanZ<T>(this._iDataPtr + startIndex, this._qDataPtr + startIndex, length);
        }

        /// <summary>
        /// 获取IQSpan泛型实例
        /// </summary>
        /// <param name="startIndex">指针数据起始位置</param>
        /// <param name="length">IQSpan长度</param>
        /// <returns>IQSpan</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public IQSpanZ<T> GetIQSpanZ(long startIndex, long length)
        {
            if (base._longCapacity - startIndex < length)
            {
                base.CheckSetLength(startIndex + length);
            }

            return new IQSpanZ<T>(this._iDataPtr + startIndex, this._qDataPtr + startIndex, length);
        }

        /// <summary>
        /// 将当前类型强制转换为目标的类型
        /// </summary>
        /// <typeparam name="TResult">目标类型</typeparam>
        /// <returns>目标类型SpanZ</returns>
        public IQSpanZ<TResult> Cast<TResult>() where TResult : struct
        {
            return new IQSpanZ<TResult>(this.IDataPtr, this.QDataPtr, this.LongLength * sizeof(T) / sizeof(TResult));
        }


        /// <summary>
        /// 获取IQSpan泛型实例
        /// </summary>
        /// <returns>IQSpan</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public LocalIQSpanZ<T> GetLocalIQSpanZ()
        {
            return new LocalIQSpanZ<T>(this._iDataPtr, this._qDataPtr, base._length);
        }

        /// <summary>
        /// 获取IQSpan泛型实例
        /// </summary>
        /// <param name="length">IQSpan长度</param>
        /// <returns>IQSpan</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public LocalIQSpanZ<T> GetLocalIQSpanZ(int length)
        {
            if (base._capacity < length)
            {
                base.CheckSetLength(length);
            }

            return new LocalIQSpanZ<T>(this._iDataPtr, this._qDataPtr, length);
        }

        /// <summary>
        /// 获取IQSpan泛型实例
        /// </summary>
        /// <param name="length">IQSpan长度</param>
        /// <returns>IQSpan</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public LocalIQSpanZ<T> GetLocalIQSpanZ(long length)
        {
            if (base._longCapacity < length)
            {
                base.CheckSetLength(length);
            }

            return new LocalIQSpanZ<T>(this._iDataPtr, this._qDataPtr, length);
        }

        /// <summary>
        /// 获取IQSpan泛型实例
        /// </summary>
        /// <param name="startIndex">指针数据起始位置</param>
        /// <param name="length">IQSpan长度</param>
        /// <returns>IQSpan</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public LocalIQSpanZ<T> GetLocalIQSpanZ(int startIndex, int length)
        {
            if (base._capacity - startIndex < length)
            {
                base.CheckSetLength(startIndex + length);
            }

            return new LocalIQSpanZ<T>(this._iDataPtr + startIndex, this._qDataPtr + startIndex, length);
        }

        /// <summary>
        /// 获取IQSpan泛型实例
        /// </summary>
        /// <param name="startIndex">指针数据起始位置</param>
        /// <param name="length">IQSpan长度</param>
        /// <returns>IQSpan</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public LocalIQSpanZ<T> GetLocalIQSpanZ(long startIndex, long length)
        {
            if (base._longCapacity - startIndex < length)
            {
                base.CheckSetLength(startIndex + length);
            }

            return new LocalIQSpanZ<T>(this._iDataPtr + startIndex, this._qDataPtr + startIndex, length);
        }






        /// <summary>
        /// 构造函数
        /// </summary>
        public IQUnmanagedMemoryPointer()
            : base(sizeof(T), 0L, UnmanagedMemoryType.Revisable)
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="elementCount">初始化元素个数</param>
        public IQUnmanagedMemoryPointer(long elementCount)
            : base(sizeof(T), elementCount, UnmanagedMemoryType.Revisable)
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="iDataPtr">I路数据指针</param>
        /// <param name="qDataPtr">Q路数据指针</param>
        /// <param name="elementCount">元素个数</param>
        public IQUnmanagedMemoryPointer(T* iDataPtr, T* qDataPtr, long elementCount)
            : base(sizeof(T), elementCount, UnmanagedMemoryType.Fix)
        {
            this._iDataPtr = iDataPtr;
            this._qDataPtr = qDataPtr;
        }







        #region get-set

        /// <summary>
        /// 获取I路指定索引处的数据
        /// </summary>
        /// <param name="index">索引</param>
        /// <returns>I路指定索引处的数据</returns>
        /// <exception cref="IndexOutOfRangeException">索引越界异常</exception>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public ref T GetI(long index)
        {
            //if (index < 0 || index >= base._longCapacity)
            //{
            //    throw new IndexOutOfRangeException();
            //}

            return ref *(this._iDataPtr + index);
        }

        /// <summary>
        /// 设置I路指定索引处的数据
        /// </summary>
        /// <param name="index">索引</param>
        /// <param name="value">目标值</param>
        /// <exception cref="IndexOutOfRangeException">索引越界异常</exception>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public void SetI(long index, in T value)
        {
            //if (index < 0 || index >= base._longCapacity)
            //{
            //    throw new IndexOutOfRangeException();
            //}

            *(this._iDataPtr + index) = value;
        }

        /// <summary>
        /// 获取I路指定索引处的数据
        /// </summary>
        /// <param name="index">索引</param>
        /// <returns>I路指定索引处的数据</returns>
        /// <exception cref="IndexOutOfRangeException">索引越界异常</exception>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public ref T GetI(int index)
        {
            //if (index < 0 || index >= base._longCapacity)
            //{
            //    throw new IndexOutOfRangeException();
            //}

            return ref *(this._iDataPtr + index);
        }

        /// <summary>
        /// 设置I路指定索引处的数据
        /// </summary>
        /// <param name="index">索引</param>
        /// <param name="value">目标值</param>
        /// <exception cref="IndexOutOfRangeException">索引越界异常</exception>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public void SetI(int index, in T value)
        {
            //if (index < 0 || index >= base._longCapacity)
            //{
            //    throw new IndexOutOfRangeException();
            //}

            *(this._iDataPtr + index) = value;
        }





        /// <summary>
        /// 获取Q路指定索引处的数据
        /// </summary>
        /// <param name="index">索引</param>
        /// <returns>Q路指定索引处的数据</returns>
        /// <exception cref="IndexOutOfRangeException">索引越界异常</exception>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public ref T GetQ(long index)
        {
            //if (index < 0 || index >= base._longCapacity)
            //{
            //    throw new IndexOutOfRangeException();
            //}

            return ref *(this._qDataPtr + index);
        }

        /// <summary>
        /// 设置Q路指定索引处的数据
        /// </summary>
        /// <param name="index">索引</param>
        /// <param name="value">目标值</param>
        /// <exception cref="IndexOutOfRangeException">索引越界异常</exception>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public void SetQ(long index, in T value)
        {
            //if (index < 0 || index >= base._longCapacity)
            //{
            //    throw new IndexOutOfRangeException();
            //}

            *(this._qDataPtr + index) = value;
        }

        /// <summary>
        /// 获取Q路指定索引处的数据
        /// </summary>
        /// <param name="index">索引</param>
        /// <returns>Q路指定索引处的数据</returns>
        /// <exception cref="IndexOutOfRangeException">索引越界异常</exception>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public ref T GetQ(int index)
        {
            //if (index < 0 || index >= base._longCapacity)
            //{
            //    throw new IndexOutOfRangeException();
            //}

            return ref *(this._qDataPtr + index);
        }

        /// <summary>
        /// 设置Q路指定索引处的数据
        /// </summary>
        /// <param name="index">索引</param>
        /// <param name="value">目标值</param>
        /// <exception cref="IndexOutOfRangeException">索引越界异常</exception>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public void SetQ(int index, in T value)
        {
            //if (index < 0 || index >= base._longCapacity)
            //{
            //    throw new IndexOutOfRangeException();
            //}

            *(this._qDataPtr + index) = value;
        }

        #endregion







        /// <summary>
        /// 将源数据复制到目标数据中,不作参数检查
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="destination">目标数据</param>
        public static void Copy<ElelmentType>(IQUnmanagedMemoryPointer<ElelmentType> source, IQUnmanagedMemoryPointer<ElelmentType> destination)
            where ElelmentType : struct
        {
            nuint byteLength = (nuint)(source._longLength * sizeof(ElelmentType));
            NativeMemory.Copy(source._iDataPtr, destination._iDataPtr, byteLength);
            NativeMemory.Copy(source._qDataPtr, destination._qDataPtr, byteLength);
        }

        /// <summary>
        /// 将源数据复制到目标数据中,不作参数检查
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="soucrceStartIndex">当前数据起始位置</param>
        /// <param name="destination">目标指针</param>
        /// <param name="destinationStartIndex">目标数据起始位置</param>
        /// <param name="count">复制元素个数</param>
        public static void Copy<ElelmentType>(IQUnmanagedMemoryPointer<ElelmentType> source, int soucrceStartIndex, IQUnmanagedMemoryPointer<ElelmentType> destination, int destinationStartIndex, int count)
            where ElelmentType : struct
        {
            nuint byteLength = new nuint((ulong)((long)count * sizeof(ElelmentType)));
            NativeMemory.Copy(source._iDataPtr + soucrceStartIndex, destination._iDataPtr + destinationStartIndex, byteLength);
            NativeMemory.Copy(source._qDataPtr + soucrceStartIndex, destination._qDataPtr + destinationStartIndex, byteLength);
        }

        /// <summary>
        /// 将源数据复制到目标数据中,不作参数检查
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="soucrceStartIndex">当前数据起始位置</param>
        /// <param name="destination">目标数据</param>
        /// <param name="destinationStartIndex">目标数据起始位置</param>
        /// <param name="count">复制元素个数</param>
        public static void Copy<ElelmentType>(IQUnmanagedMemoryPointer<ElelmentType> source, long soucrceStartIndex, IQUnmanagedMemoryPointer<ElelmentType> destination, long destinationStartIndex, long count)
            where ElelmentType : struct
        {
            nuint byteLength = (nuint)(count * sizeof(ElelmentType));
            NativeMemory.Copy(source._iDataPtr + soucrceStartIndex, destination._iDataPtr + destinationStartIndex, byteLength);
            NativeMemory.Copy(source._qDataPtr + soucrceStartIndex, destination._qDataPtr + destinationStartIndex, byteLength);
        }

        /// <summary>
        /// 将源数据复制到目标数据中,不作参数检查
        /// </summary>
        /// <param name="sourceIData">数据源I路</param>
        /// <param name="sourceQData">数据源Q路</param>
        /// <param name="soucrceStartIndex">当前数据起始位置</param>
        /// <param name="destination">目标数据</param>
        /// <param name="destinationStartIndex">目标数据起始位置</param>
        /// <param name="count">复制元素个数</param>
        public static void Copy<ElelmentType>(ElelmentType[] sourceIData, ElelmentType[] sourceQData, int soucrceStartIndex, IQUnmanagedMemoryPointer<ElelmentType> destination, long destinationStartIndex, long count)
            where ElelmentType : struct
        {
            fixed (ElelmentType* ptrI = sourceIData, ptrQ = sourceQData)
            {
                nuint byteCount = new nuint((ulong)(count * sizeof(ElelmentType)));
                NativeMemory.Copy(ptrI + soucrceStartIndex, destination._iDataPtr + destinationStartIndex, byteCount);
                NativeMemory.Copy(ptrQ + soucrceStartIndex, destination._qDataPtr + destinationStartIndex, byteCount);
            }
        }

        /// <summary>
        /// 将源数据复制到目标数据中,不作参数检查
        /// </summary>
        /// <param name="sourceIData">数据源I路</param>
        /// <param name="sourceQData">数据源Q路</param>
        /// <param name="soucrceStartIndex">当前数据起始位置</param>
        /// <param name="destination">目标数据</param>
        /// <param name="destinationStartIndex">目标数据起始位置</param>
        /// <param name="count">复制元素个数</param>
        public static void Copy<ElelmentType>(ElelmentType[] sourceIData, ElelmentType[] sourceQData, long soucrceStartIndex, IQUnmanagedMemoryPointer<ElelmentType> destination, long destinationStartIndex, long count)
            where ElelmentType : struct
        {
            fixed (ElelmentType* ptrI = sourceIData, ptrQ = sourceQData)
            {
                nuint byteCount = new nuint((ulong)(count * sizeof(ElelmentType)));
                NativeMemory.Copy(ptrI + soucrceStartIndex, destination._iDataPtr + destinationStartIndex, byteCount);
                NativeMemory.Copy(ptrQ + soucrceStartIndex, destination._qDataPtr + destinationStartIndex, byteCount);
            }
        }





        /// <summary>
        /// 将当前数据复制到目标数据中,作参数检查
        /// </summary>
        /// <param name="destination">目标数据</param>
        public void CopyTo(IQUnmanagedMemoryPointer<T> destination)
        {
            if (destination == null)
            {
                throw new ArgumentOutOfRangeException(nameof(destination));
            }

            if (destination._longCapacity < base._longLength)
            {
                throw new ArgumentOutOfRangeException(nameof(destination), "目标容量不足");
            }

            nuint byteLength = (nuint)(base._longLength * sizeof(T));
            NativeMemory.Copy(this._iDataPtr, destination._iDataPtr, byteLength);
            NativeMemory.Copy(this._qDataPtr, destination._qDataPtr, byteLength);
        }

        /// <summary>
        /// 将当前数据复制到目标数据中,作参数检查
        /// </summary>
        /// <param name="soucrceStartIndex">当前数据起始位置</param>
        /// <param name="destination">目标数据</param>
        /// <param name="destinationStartIndex">目标数据起始位置</param>
        /// <param name="count">复制元素个数</param>
        public void CopyTo(int soucrceStartIndex, IQUnmanagedMemoryPointer<T> destination, int destinationStartIndex, int count)
        {
            if (soucrceStartIndex < 0 || soucrceStartIndex >= base._length)
            {
                throw new ArgumentOutOfRangeException(nameof(soucrceStartIndex), "复制的源数据起始位置超出了当前数据容量范围");
            }

            long soucrceStartIndexL = soucrceStartIndex;
            long countL = count;
            if (soucrceStartIndexL + countL > base._longLength)
            {
                throw new ArgumentOutOfRangeException(nameof(count), "复制的数据长度超出了当前数据容量");
            }

            if (destination == null)
            {
                throw new ArgumentOutOfRangeException(nameof(destination));
            }

            long destinationStartIndexL = destinationStartIndex;
            if (destinationStartIndexL < 0 || destinationStartIndexL >= destination._longCapacity)
            {
                throw new ArgumentOutOfRangeException(nameof(destinationStartIndex), "复制的目标数据起始位置超出了当前数据容量范围");
            }

            if (destinationStartIndex + countL > destination._longCapacity)
            {
                throw new ArgumentOutOfRangeException(nameof(count), "复制的数据长度超出了目标数据容量");
            }

            nuint byteLength = new nuint((ulong)(countL * sizeof(T)));
            NativeMemory.Copy(this._iDataPtr + soucrceStartIndexL, destination._iDataPtr + destinationStartIndexL, byteLength);
            NativeMemory.Copy(this._qDataPtr + soucrceStartIndexL, destination._qDataPtr + destinationStartIndexL, byteLength);
        }

        /// <summary>
        /// 将当前数据复制到目标数据中,作参数检查
        /// </summary>
        /// <param name="soucrceStartIndex">当前数据起始位置</param>
        /// <param name="destination">目标数据</param>
        /// <param name="destinationStartIndex">目标数据起始位置</param>
        /// <param name="count">复制元素个数</param>
        public void CopyTo(long soucrceStartIndex, IQUnmanagedMemoryPointer<T> destination, long destinationStartIndex, long count)
        {
            if (soucrceStartIndex < 0 || soucrceStartIndex >= base._longLength)
            {
                throw new ArgumentOutOfRangeException(nameof(soucrceStartIndex), "复制的源数据起始位置超出了当前数据容量范围");
            }

            if (soucrceStartIndex + count > base._longLength)
            {
                throw new ArgumentOutOfRangeException(nameof(count), "复制的数据长度超出了当前数据容量");
            }

            if (destination == null)
            {
                throw new ArgumentOutOfRangeException(nameof(destination));
            }

            if (destinationStartIndex < 0 || destinationStartIndex >= destination._longCapacity)
            {
                throw new ArgumentOutOfRangeException(nameof(destinationStartIndex), "复制的目标数据起始位置超出了当前数据容量范围");
            }

            if (destinationStartIndex + count > destination._longCapacity)
            {
                throw new ArgumentOutOfRangeException(nameof(count), "复制的数据长度超出了目标数据容量");
            }

            nuint byteLength = new nuint((ulong)(count * sizeof(T)));
            NativeMemory.Copy(this._iDataPtr + soucrceStartIndex, destination._iDataPtr + destinationStartIndex, byteLength);
            NativeMemory.Copy(this._qDataPtr + soucrceStartIndex, destination._qDataPtr + destinationStartIndex, byteLength);
        }






        /// <summary>
        /// 调整指针指向的内存大小,调整成功返回true,调整失败返回false
        /// </summary>
        /// <param name="byteLength">要申请的内存大小字节数</param>
        /// <param name="error">调整失败时的错误信息</param>
        /// <returns>调整成功返回true,调整失败返回false</returns>
        protected override bool PrimitiveCheck(long byteLength, out string error)
        {
            void* newIDataPtr, newQDataPtr;
            var byteCount = new nuint((ulong)byteLength);
            if (this._iDataPtr != null)
            {
                newIDataPtr = NativeMemory.Realloc(this._iDataPtr, byteCount);
                newQDataPtr = NativeMemory.Realloc(this._qDataPtr, byteCount);
            }
            else
            {
                newIDataPtr = NativeMemory.Alloc(byteCount);
                newQDataPtr = NativeMemory.Alloc(byteCount);
            }

            if (newIDataPtr == null)
            {
                if (newQDataPtr == null)
                {
                    error = $"I路和Q路申请大小为{byteLength}Byte的内存失败.";
                    return false;
                }
                else
                {
                    NativeMemory.Free(newQDataPtr);
                    error = $"I路申请大小为{byteLength}Byte的内存失败.";
                    return false;
                }
            }
            else
            {
                if (newQDataPtr == null)
                {
                    NativeMemory.Free(newIDataPtr);
                    error = $"Q路申请大小为{byteLength}Byte的内存失败.";
                    return false;
                }
                else
                {
                    this._iDataPtr = (T*)newIDataPtr;
                    this._qDataPtr = (T*)newQDataPtr;
                    error = null;
                    return true;
                }
            }
        }

        /// <summary>
        /// 释放指针
        /// </summary>
        protected override void PrimitiveFree()
        {
            if (this._iDataPtr != null)
            {
                NativeMemory.Free(this._iDataPtr);
                this._iDataPtr = null;
            }

            if (this._qDataPtr != null)
            {
                NativeMemory.Free(this._qDataPtr);
                this._qDataPtr = null;
            }
        }

        /// <summary>
        /// 清除当前对象数据指针指向的内容。
        /// </summary>
        protected override void PrimitiveClear()
        {
            var byteLen = (nuint)(base._elementSize * base._length);
            NativeMemory.Clear(this._iDataPtr, byteLen);
            NativeMemory.Clear(this._qDataPtr, byteLen);
        }

        /// <summary>
        /// 用指定的值填充到当前对象数据指针指向的内容
        /// </summary>
        /// <typeparam name="TValue">值类型</typeparam>
        /// <param name="value"></param>        
#if CORE
        protected override void PrimitiveFill<TValue>(TValue value) where TValue : struct
#else
        protected override void PrimitiveFill<TValue>(TValue value)
#endif
        {
            UnmanagedMemoryHelper.Fill(this._iDataPtr, base._length, value);
            UnmanagedMemoryHelper.Fill(this._qDataPtr, base._length, value);
        }

        /// <summary>
        /// 重写ToString
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.GetIQSpanZ().ToString();
        }
    }

    /// <summary>
    /// 非托管内存指针
    /// </summary>
    [DebuggerDisplay("Length = {Length}")]
    unsafe public sealed class UnmanagedMemoryPointer : UnmanagedMemoryPointerAbs
    {
        /// <summary>
        /// 指针
        /// </summary>
        private void* _dataPtr = null;
        /// <summary>
        /// 指针
        /// </summary>
        public void* DataPtr
        {
#if !NET4_0
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
            get { return this._dataPtr; }
        }




        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="elementSize">元素大小</param>
        public UnmanagedMemoryPointer(int elementSize)
            : base(elementSize, 0L, UnmanagedMemoryType.Revisable)
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="elementSize">元素大小</param>
        /// <param name="count">初始化元素个数</param>
        public UnmanagedMemoryPointer(int elementSize, int count)
            : base(elementSize, count, UnmanagedMemoryType.Revisable)
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dataPtr">指针</param>
        /// <param name="elementSize">元素大小</param>
        /// <param name="elementCount">元素个数</param>
        public UnmanagedMemoryPointer(void* dataPtr, int elementSize, long elementCount)
            : base(elementSize, elementCount, UnmanagedMemoryType.Fix)
        {
            this._dataPtr = dataPtr;
        }





        #region 泛型操作

        /// <summary>
        /// 获取Span泛型实例
        /// </summary>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public Span<T> GetSpan<T>() where T : struct
        {
            long count = base._longLength * base._elementSize / sizeof(T);
            if (count > int.MaxValue)
            {
                return new Span<T>(this._dataPtr, int.MaxValue);
            }
            else
            {
                return new Span<T>(this._dataPtr, (int)count);
            }
        }

        /// <summary>
        /// 获取Span泛型实例
        /// </summary>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public Span<T> GetSpan<T>(int length) where T : struct
        {
            int genericEleSize = sizeof(T);
            if (genericEleSize == base._elementSize)
            {
                if (length > base._capacity)
                {
                    base.CheckSetLength(length);
                }
            }
            else
            {
                long requirByteLength = length * sizeof(T);
                long hasByteLength = base._elementSize * base._longCapacity;
                if (requirByteLength > hasByteLength)
                {
                    base.CheckSetLength(requirByteLength / base._elementSize + 1);
                }
            }

            return new Span<T>(this._dataPtr, length);
        }




        /// <summary>
        /// 获取SpanZ泛型实例
        /// </summary>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public SpanZ<T> GetSpanZ<T>() where T : struct
        {
            long count = base._longLength * base._elementSize / sizeof(T);
            return new SpanZ<T>((T*)this._dataPtr, count);
        }

        /// <summary>
        /// 获取SpanZ泛型实例
        /// </summary>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public SpanZ<T> GetSpanZ<T>(int length) where T : struct
        {
            int genericEleSize = sizeof(T);
            if (genericEleSize == base._elementSize)
            {
                if (length > base._capacity)
                {
                    base.CheckSetLength(length);
                }
            }
            else
            {
                long requirByteLength = length * genericEleSize;
                long hasByteLength = base._elementSize * base._longCapacity;
                if (requirByteLength > hasByteLength)
                {
                    base.CheckSetLength(requirByteLength / base._elementSize + 1);
                }
            }

            return new SpanZ<T>((T*)this._dataPtr, length);
        }

        /// <summary>
        /// 获取SpanZ泛型实例
        /// </summary>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public SpanZ<T> GetSpanZ<T>(long length) where T : struct
        {
            int genericEleSize = sizeof(T);
            if (genericEleSize == base._elementSize)
            {
                if (length > base._longCapacity)
                {
                    base.CheckSetLength(length);
                }
            }
            else
            {
                long requirByteLength = length * genericEleSize;
                long hasByteLength = base._elementSize * base._longCapacity;
                if (requirByteLength > hasByteLength)
                {
                    base.CheckSetLength(requirByteLength / base._elementSize + 1);
                }
            }

            return new SpanZ<T>((T*)this._dataPtr, length);
        }








        /// <summary>
        /// 获取指定索引处的值
        /// </summary>
        /// <typeparam name="T">值类型</typeparam>
        /// <param name="index">索引</param>
        /// <returns>指定索引处的值</returns>
        /// <exception cref="IndexOutOfRangeException">索引越界异常</exception>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public ref T GetValue<T>(long index) where T : struct
        {
            if (index < 0 || index >= base._longCapacity)
            {
                throw new ArgumentOutOfRangeException(nameof(index), $"索引越界,index:{index},当前数据容量:{base._longCapacity}");
            }

            return ref *((T*)this._dataPtr + index);
        }

        /// <summary>
        /// 设置指定索引处的值
        /// </summary>
        /// <typeparam name="T">值类型</typeparam>
        /// <param name="index">索引</param>
        /// <param name="value">索</param>
        /// <exception cref="IndexOutOfRangeException">索引越界异常</exception>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public void SetValue<T>(long index, in T value) where T : struct
        {
            if (index < 0 || index >= base._longCapacity)
            {
                throw new ArgumentOutOfRangeException(nameof(index), $"索引越界,index:{index},当前数据容量:{base._longCapacity}");
            }

            *((T*)this._dataPtr + index) = value;
        }


        /// <summary>
        /// 获取指定索引处的值
        /// </summary>
        /// <param name="index">索引</param>
        /// <returns>指定索引处的值</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public ref T GetValue<T>(int index) where T : struct
        {
            if (index < 0 || index >= base._longCapacity)
            {
                throw new ArgumentOutOfRangeException(nameof(index), $"索引越界,index:{index},当前数据容量:{base._longCapacity}");
            }

            return ref *((T*)this._dataPtr + index);
        }

        /// <summary>
        /// 设置指定索引处的值
        /// </summary>
        /// <typeparam name="T">值类型</typeparam>
        /// <param name="index">索引</param>
        /// <param name="value">索</param>
        /// <exception cref="IndexOutOfRangeException">索引越界异常</exception>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public void SetValue<T>(int index, in T value) where T : struct
        {
            if (index < 0 || index >= base._longCapacity)
            {
                throw new ArgumentOutOfRangeException(nameof(index), $"索引越界,index:{index},当前数据容量:{base._longCapacity}");
            }

            *((T*)this._dataPtr + index) = value;
        }

        #endregion




        /// <summary>
        /// 调整指针指向的内存大小,调整成功返回true,调整失败返回false
        /// </summary>
        /// <param name="byteLength">要申请的内存大小字节数</param>
        /// <param name="error">调整失败时的错误信息</param>
        /// <returns>调整成功返回true,调整失败返回false</returns>
        protected override bool PrimitiveCheck(long byteLength, out string error)
        {
            void* newPtr;
            var byteCount = new nuint((ulong)byteLength);

            if (this._dataPtr == null)
            {
                newPtr = NativeMemory.Alloc(byteCount);
            }
            else
            {
                newPtr = NativeMemory.Realloc(this._dataPtr, byteCount);
            }

            if (newPtr == null)
            {
                error = $"申请大小为{byteLength}Byte的内存失败.";
                return false;
            }
            else
            {
                this._dataPtr = newPtr;
                error = null;
                return true;
            }
        }

        /// <summary>
        /// 释放指针
        /// </summary>
        protected override void PrimitiveFree()
        {
            if (this._dataPtr != null)
            {
                NativeMemory.Free(this._dataPtr);
                this._dataPtr = null;
            }
        }

        /// <summary>
        /// 清除当前对象数据指针指向的内容。
        /// </summary>
        protected override void PrimitiveClear()
        {
            NativeMemory.Clear(this._dataPtr, (nuint)(base._elementSize * base._longCapacity));
        }

        /// <summary>
        /// 用指定的值填充到当前对象数据指针指向的内容
        /// </summary>
        /// <typeparam name="TValue">值类型</typeparam>
        /// <param name="value"></param>
#if CORE
        protected override void PrimitiveFill<TValue>(TValue value) where TValue : struct
#else
        protected override void PrimitiveFill<TValue>(TValue value)
#endif
        {
            UnmanagedMemoryHelper.Fill(this._dataPtr, base._longCapacity, value);
        }

    }

    /// <summary>
    /// IQ数据非托管内存指针
    /// </summary>
    [DebuggerDisplay("Length = {Length}")]
    unsafe public sealed class IQUnmanagedMemoryPointer : UnmanagedMemoryPointerAbs
    {
        /// <summary>
        /// I路数据指针
        /// </summary>
        private void* _iDataPtr = null;
        /// <summary>
        /// I路数据指针
        /// </summary>
        public void* IDataPtr
        {
#if !NET4_0
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
            get { return this._iDataPtr; }
        }



        /// <summary>
        /// Q路数据指针
        /// </summary>
        private void* _qDataPtr = null;
        /// <summary>
        /// Q路数据指针
        /// </summary>
        public void* QDataPtr
        {
#if !NET4_0
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
            get { return this._qDataPtr; }
        }



        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="elementSize">元素大小</param>
        public IQUnmanagedMemoryPointer(int elementSize)
            : base(elementSize, 0L, UnmanagedMemoryType.Revisable)
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="elementSize">元素大小</param>
        /// <param name="count">初始化元素个数</param>
        public IQUnmanagedMemoryPointer(int elementSize, int count)
            : base(elementSize, count, UnmanagedMemoryType.Revisable)
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="iDataPtr">I路数据指针</param>
        /// <param name="qDataPtr">Q路数据指针</param>
        /// <param name="elementSize">元素大小</param>
        /// <param name="elementCount">元素个数</param>
        public IQUnmanagedMemoryPointer(void* iDataPtr, void* qDataPtr, int elementSize, long elementCount)
            : base(elementSize, elementCount, UnmanagedMemoryType.Fix)
        {
            this._iDataPtr = iDataPtr;
            this._qDataPtr = qDataPtr;
        }






        #region 泛型操作


        /// <summary>
        /// 获取Length长度的I路Span泛型实例
        /// </summary>
        /// <typeparam name="T">目标数据类型</typeparam>
        /// <returns>QSpan泛型实例</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public Span<T> GetISpan<T>() where T : struct
        {
            int genericEleSize = sizeof(T);
            if (genericEleSize == base._elementSize)
            {
                return new Span<T>(this._iDataPtr, base._length);
            }
            else
            {
                var count = base._longLength * base._elementSize / genericEleSize;
                if (count > int.MaxValue)
                {
                    return new Span<T>(this._iDataPtr, int.MaxValue);
                }
                else
                {
                    return new Span<T>(this._iDataPtr, (int)count);
                }
            }
        }

        /// <summary>
        /// 获取I路Span泛型实例
        /// </summary>
        /// <typeparam name="T">目标数据类型</typeparam>
        /// <param name="length">数据长度</param>
        /// <returns>ISpan泛型实例</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public Span<T> GetISpan<T>(int length) where T : struct
        {
            int genericEleSize = sizeof(T);
            if (genericEleSize == base._elementSize)
            {
                if (length > base._capacity)
                {
                    base.CheckSetLength(length);
                }
            }
            else
            {
                long requirByteLength = length * sizeof(T);
                long hasByteLength = base._elementSize * base._longCapacity;
                if (requirByteLength > hasByteLength)
                {
                    base.CheckSetLength(requirByteLength / base._elementSize + 1);
                }
            }

            return new Span<T>(this._iDataPtr, length);
        }

        /// <summary>
        /// 获取I路Span泛型实例
        /// </summary>
        /// <typeparam name="T">目标数据类型</typeparam>
        /// <param name="length">数据长度</param>
        /// <returns>ISpan泛型实例</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public SpanZ<T> GetISpanZ<T>(long length) where T : struct
        {
            int genericEleSize = sizeof(T);
            if (genericEleSize == base._elementSize)
            {
                if (length > base._capacity)
                {
                    base.CheckSetLength(length);
                }
            }
            else
            {
                long requirByteLength = length * sizeof(T);
                long hasByteLength = base._elementSize * base._longCapacity;
                if (requirByteLength > hasByteLength)
                {
                    base.CheckSetLength(requirByteLength / base._elementSize + 1);
                }
            }

            return new SpanZ<T>((T*)this._iDataPtr, length);
        }




        /// <summary>
        /// 获取Length长度的Q路Span泛型实例
        /// </summary>
        /// <typeparam name="T">目标数据类型</typeparam>
        /// <returns>QSpan泛型实例</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public Span<T> GetQSpan<T>() where T : struct
        {
            int genericEleSize = sizeof(T);
            if (genericEleSize == base._elementSize)
            {
                return new Span<T>(this._qDataPtr, base._length);
            }
            else
            {
                var count = base._longLength * base._elementSize / genericEleSize;
                if (count > int.MaxValue)
                {
                    return new Span<T>(this._qDataPtr, int.MaxValue);
                }
                else
                {
                    return new Span<T>(this._qDataPtr, (int)count);
                }
            }
        }

        /// <summary>
        /// 获取Q路Span泛型实例
        /// </summary>
        /// <typeparam name="T">目标数据类型</typeparam>
        /// <param name="length">数据长度</param>
        /// <returns>QSpan泛型实例</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public Span<T> GetQSpan<T>(int length) where T : struct
        {
            int genericEleSize = sizeof(T);
            if (genericEleSize == base._elementSize)
            {
                if (length > base._capacity)
                {
                    base.CheckSetLength(length);
                }
            }
            else
            {
                long requirByteLength = length * sizeof(T);
                long hasByteLength = base._elementSize * base._longCapacity;
                if (requirByteLength > hasByteLength)
                {
                    base.CheckSetLength(requirByteLength / base._elementSize + 1);
                }
            }

            return new Span<T>(this._qDataPtr, length);
        }

        /// <summary>
        /// 获取Q路Span泛型实例
        /// </summary>
        /// <typeparam name="T">目标数据类型</typeparam>
        /// <param name="length">数据长度</param>
        /// <returns>QSpan泛型实例</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public SpanZ<T> GetQSpanZ<T>(long length) where T : struct
        {
            int genericEleSize = sizeof(T);
            if (genericEleSize == base._elementSize)
            {
                if (length > base._capacity)
                {
                    base.CheckSetLength(length);
                }
            }
            else
            {
                long requirByteLength = length * sizeof(T);
                long hasByteLength = base._elementSize * base._longCapacity;
                if (requirByteLength > hasByteLength)
                {
                    base.CheckSetLength(requirByteLength / base._elementSize + 1);
                }
            }

            return new SpanZ<T>((T*)this._qDataPtr, length);
        }



        /// <summary>
        /// 获取Length长度的IQSpan泛型实例
        /// </summary>
        /// <typeparam name="T">目标数据类型</typeparam>
        /// <returns>IQSpan泛型实例</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public IQSpanZ<T> GetIQSpanZ<T>() where T : struct
        {
            return new IQSpanZ<T>((T*)this._iDataPtr, (T*)this._qDataPtr, (int)(base._longLength * base._elementSize / sizeof(T)));
        }

        /// <summary>
        /// 获取IQSpan泛型实例
        /// </summary>
        /// <typeparam name="T">目标数据类型</typeparam>
        /// <param name="length">数据长度</param>
        /// <returns>IQSpan泛型实例</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public IQSpanZ<T> GetIQSpanZ<T>(int length) where T : struct
        {
            int genericEleSize = sizeof(T);
            if (genericEleSize == base._elementSize)
            {
                if (length > base._capacity)
                {
                    base.CheckSetLength(length);
                }
            }
            else
            {
                long requirByteLength = length * sizeof(T);
                long hasByteLength = base._elementSize * base._longCapacity;
                if (requirByteLength > hasByteLength)
                {
                    base.CheckSetLength(requirByteLength / base._elementSize + 1);
                }
            }

            return new IQSpanZ<T>((T*)this._iDataPtr, (T*)this._qDataPtr, length);
        }

        /// <summary>
        /// 获取IQSpan泛型实例
        /// </summary>
        /// <typeparam name="T">目标数据类型</typeparam>
        /// <param name="length">数据长度</param>
        /// <returns>IQSpan泛型实例</returns>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public IQSpanZ<T> GetIQSpanZ<T>(long length) where T : struct
        {
            int genericEleSize = sizeof(T);
            if (genericEleSize == base._elementSize)
            {
                if (length > base._longCapacity)
                {
                    base.CheckSetLength(length);
                }
            }
            else
            {
                long requirByteLength = length * sizeof(T);
                long hasByteLength = base._elementSize * base._longCapacity;
                if (requirByteLength > hasByteLength)
                {
                    base.CheckSetLength(requirByteLength / base._elementSize + 1);
                }
            }

            return new IQSpanZ<T>((T*)this._iDataPtr, (T*)this._qDataPtr, length);
        }










        /// <summary>
        /// 获取I路指定索引处的数据
        /// </summary>
        /// <param name="index">索引</param>
        /// <returns>I路指定索引处的数据</returns>
        /// <exception cref="IndexOutOfRangeException">索引越界异常</exception>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public ref T GetI<T>(long index) where T : struct
        {
            //if (index < 0 || index * sizeof(T) >= base._longCapacity)
            //{
            //    throw new IndexOutOfRangeException();
            //}

            return ref *((T*)this._iDataPtr + index);
        }

        /// <summary>
        /// 设置I路指定索引处的数据
        /// </summary>
        /// <param name="index">索引</param>
        /// <param name="value">目标值</param>
        /// <exception cref="IndexOutOfRangeException">索引越界异常</exception>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public void SetI<T>(long index, in T value) where T : struct
        {
            //if (index < 0 || index * sizeof(T) >= base._longCapacity)
            //{
            //    throw new IndexOutOfRangeException();
            //}

            *((T*)this._iDataPtr + index) = value;
        }

        /// <summary>
        /// 获取I路指定索引处的数据
        /// </summary>
        /// <param name="index">索引</param>
        /// <returns>I路指定索引处的数据</returns>
        /// <exception cref="IndexOutOfRangeException">索引越界异常</exception>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public ref T GetI<T>(int index) where T : struct
        {
            //if (index < 0 || index * sizeof(T) >= base._longCapacity)
            //{
            //    throw new IndexOutOfRangeException();
            //}

            return ref *((T*)this._iDataPtr + index);
        }

        /// <summary>
        /// 设置I路指定索引处的数据
        /// </summary>
        /// <param name="index">索引</param>
        /// <param name="value">目标值</param>
        /// <exception cref="IndexOutOfRangeException">索引越界异常</exception>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public void SetI<T>(int index, T value) where T : struct
        {
            //if (index < 0 || index * sizeof(T) >= base._longCapacity)
            //{
            //    throw new IndexOutOfRangeException();
            //}

            *((T*)this._iDataPtr + index) = value;
        }





        /// <summary>
        /// 获取Q路指定索引处的数据
        /// </summary>
        /// <param name="index">索引</param>
        /// <returns>Q路指定索引处的数据</returns>
        /// <exception cref="IndexOutOfRangeException">索引越界异常</exception>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public ref T GetQ<T>(long index) where T : struct
        {
            //if (index < 0 || index * sizeof(T) >= base._longCapacity)
            //{
            //    throw new IndexOutOfRangeException();
            //}

            return ref *((T*)this._qDataPtr + index);
        }

        /// <summary>
        /// 设置Q路指定索引处的数据
        /// </summary>
        /// <param name="index">索引</param>
        /// <param name="value">目标值</param>
        /// <exception cref="IndexOutOfRangeException">索引越界异常</exception>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public void SetQ<T>(long index, in T value) where T : struct
        {
            //if (index < 0 || index * sizeof(T) >= base._longCapacity)
            //{
            //    throw new IndexOutOfRangeException();
            //}

            *((T*)this._qDataPtr + index) = value;
        }

        /// <summary>
        /// 获取Q路指定索引处的数据
        /// </summary>
        /// <param name="index">索引</param>
        /// <returns>Q路指定索引处的数据</returns>
        /// <exception cref="IndexOutOfRangeException">索引越界异常</exception>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public ref T GetQ<T>(int index) where T : struct
        {
            //if (index < 0 || index * sizeof(T) >= base._longCapacity)
            //{
            //    throw new IndexOutOfRangeException();
            //}

            return ref *((T*)this._qDataPtr + index);
        }

        /// <summary>
        /// 设置Q路指定索引处的数据
        /// </summary>
        /// <param name="index">索引</param>
        /// <param name="value">目标值</param>
        /// <exception cref="IndexOutOfRangeException">索引越界异常</exception>
#if !NET4_0
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
        public void SetQ<T>(int index, in T value) where T : struct
        {
            //if (index < 0 || index * sizeof(T) >= base._longCapacity)
            //{
            //    throw new IndexOutOfRangeException();
            //}

            *((T*)this._qDataPtr + index) = value;
        }

        #endregion








        /// <summary>
        /// 调整指针指向的内存大小,调整成功返回true,调整失败返回false
        /// </summary>
        /// <param name="byteLength">要申请的内存大小字节数</param>
        /// <param name="error">调整失败时的错误信息</param>
        /// <returns>调整成功返回true,调整失败返回false</returns>
        protected override bool PrimitiveCheck(long byteLength, out string error)
        {
            void* newIDataPtr, newQDataPtr;
            var byteCount = new nuint((ulong)byteLength);
            if (this._iDataPtr != null)
            {
                newIDataPtr = NativeMemory.Realloc(this._iDataPtr, byteCount);
                newQDataPtr = NativeMemory.Realloc(this._qDataPtr, byteCount);
            }
            else
            {
                newIDataPtr = NativeMemory.Alloc(byteCount);
                newQDataPtr = NativeMemory.Alloc(byteCount);
            }

            if (newIDataPtr == null)
            {
                if (newQDataPtr == null)
                {
                    error = $"I路和Q路申请大小为{byteLength}Byte的内存失败.";
                    return false;
                }
                else
                {
                    NativeMemory.Free(newQDataPtr);
                    error = $"I路申请大小为{byteLength}Byte的内存失败.";
                    return false;
                }
            }
            else
            {
                if (newQDataPtr == null)
                {
                    NativeMemory.Free(newIDataPtr);
                    error = $"Q路申请大小为{byteLength}Byte的内存失败.";
                    return false;
                }
                else
                {
                    this._iDataPtr = newIDataPtr;
                    this._qDataPtr = newQDataPtr;
                    error = null;
                    return true;
                }
            }
        }

        /// <summary>
        /// 释放指针
        /// </summary>
        protected override void PrimitiveFree()
        {
            if (this._iDataPtr != null)
            {
                NativeMemory.Free(this._iDataPtr);
                this._iDataPtr = null;
            }

            if (this._qDataPtr != null)
            {
                NativeMemory.Free(this._qDataPtr);
                this._qDataPtr = null;
            }
        }

        /// <summary>
        /// 清除当前对象数据指针指向的内容。
        /// </summary>
        protected override void PrimitiveClear()
        {
            var byteLen = (nuint)(base._elementSize * base._longCapacity);
            NativeMemory.Clear(this._iDataPtr, byteLen);
            NativeMemory.Clear(this._qDataPtr, byteLen);
        }

        /// <summary>
        /// 用指定的值填充到当前对象数据指针指向的内容
        /// </summary>
        /// <typeparam name="TValue">值类型</typeparam>
        /// <param name="value"></param>        
#if CORE
        protected override void PrimitiveFill<TValue>(TValue value) where TValue : struct
#else
        protected override void PrimitiveFill<TValue>(TValue value)
#endif
        {
            UnmanagedMemoryHelper.Fill(this._iDataPtr, base._longCapacity, value);
            UnmanagedMemoryHelper.Fill(this._qDataPtr, base._longCapacity, value);
        }

    }

}
